package book_management.user.book;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolver.iterator;

//书架类
public class BookList {
 //使用JDK内置的动态数组
 //保存每个书籍的对象
 private static List<Book> books = new ArrayList<>();
 //保存所有书籍的名称，作为操作书籍的索引
 private static List<String> booksName = new ArrayList<>();

 static {
  //初始化操作
  books.add(new Book("水浒传", "施耐庵", 66.6, "武侠小说"));
  books.add(new Book("三国演义", "罗贯中", 80.6, "谋略小说"));
  books.add(new Book("西游记", "吴承恩", 76.6, "玄幻小说"));
  books.add(new Book("红楼梦", "曹雪芹", 166.6, "牛逼小说"));

  booksName.add("水浒传");
  booksName.add("三国演义");
  booksName.add("西游记");
  booksName.add("红楼梦");
 }

 public boolean isContains(String bookName) {
  return booksName.contains(bookName);
 }

 public void bookAdd(Book book) {
  books.add(book);
  booksName.add(book.getName());
 }

 public void printBook() {
  //遍历books数组
  Iterator<Book> iterator = books.iterator();
  while (iterator.hasNext()) {
   Book book = iterator.next();
   System.out.println(book);
  }
 }

 private Book findBook(String booksName) {
  Iterator<Book> iterator = books.iterator();
  while (iterator.hasNext()) {
   Book book = iterator.next();
   if (book.getName().equals(booksName)) {
    return book;
   }
  }
  return null;
 }

 public void updateBook(String booksName, double price, String type) {
  Book book = findBook(booksName);
  book.setPrice(price);
  book.setType(type);
 }

 public void bookBorrow(String booksName) {
  Book book = findBook(booksName);
  book.setBorrowed(true);
 }

 public void bookRemove(String booksName) {
  Book book = findBook(booksName);
  books.remove(book);
  this.booksName.remove(booksName);
 }

 public void bookReturn(String booksName) {
  Book book = findBook(booksName);
  book.setBorrowed(false);
 }

 public boolean isBorrowed(String booksName) {
  Book book = findBook(booksName);
  return book.isBorrowed();
 }
}